define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        _queryString: '',
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'ycmx/ycmx/index' + Controller._queryString,
                    add_url: 'ycmx/ycmx/add' + Controller._queryString,
                    edit_url: 'ycmx/ycmx/edit',
                    del_url: 'ycmx/ycmx/del',
                    multi_url: 'ycmx/ycmx/multi',
                    import_url: 'ycmx/ycmx/import',
                    table: 'ycmx',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { checkbox: true },
                        {
                            field: '', title: __('序号'), formatter: function (value, row, index) {
                                var options = table.bootstrapTable('getOptions');
                                var pageNumber = options.pageNumber;

                                var pageSize = options.pageSize == "All" ? 100000 : options.pageSize;

                                return (pageNumber - 1) * pageSize + 1 + index;
                            }, operate: false
                        },
                        { field: 'cz', title: __('Cz'), operate: 'LIKE', operate: 'FIND_IN_SET', addclass: 'selectpage', extend: 'data-source="ycmx/cz"  data-primary-key="name"  data-select-only="true"' },
                        { field: 'ch', title: __('Ch'),sortable: true, operate: 'LIKE' },
                        { field: 'jsy', title: __('Jsy'), operate: 'LIKE' },
                        { field: 'ksdate', title: __('Ksdate'),sortable: true, operate: 'RANGE', addclass: 'datetimerange', autocomplete: false },
                        { field: 'jsdate', title: __('Jsdate'), sortable: true,operate: 'RANGE', addclass: 'datetimerange', autocomplete: false },
                        { field: 'qd', title: __('Qd'), operate: 'LIKE' },
                        { field: 'mdd', title: __('Mdd'), operate: 'LIKE' },
                        { field: 'zc', title: __('Zc'), operate: 'LIKE', operate: 'FIND_IN_SET', addclass: 'selectpage', extend: 'data-source="ycmx/zcdw"  data-primary-key="name"  data-select-only="true"' },
                        { field: 'lxr', title: __('Lxr'), operate: 'LIKE' },
                        { field: 'dh', title: __('Dh'), operate: 'LIKE' },
                        { field: 'je', title: __('Je'), operate: 'BETWEEN' },
                        { field: 'bz', title: __('Bz'), operate: false },
                        {field: 'tadmin.username', title: __('记录人'),operate: false},
                        { field: 'status', title: __('Status'), sortable: true,searchList: { "未结账": __('未结账'), "已结账": __('已结账') }, formatter: Table.api.formatter.status },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, visible: false },
                        { field: 'updatetime', title: __('Updatetime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, visible: false },

                        { field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'ycmx/ycmx/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'name', title: __('Name') },
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'ycmx/ycmx/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'ycmx/ycmx/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $("#c-lxr").data("params", function (obj) {

                    return { custom: { pid: $("#c-zc").val() } };
                });
                //当联系人改变时，查询记录，并填充电话数据
                $("#c-lxr").on("change", function () {
                    var name = $("#c-lxr").val();
                    // console.log(name);
                    Fast.api.ajax({
                        url: 'ycmx/lxr/dh',
                        data: { name: name }
                    }, function (data,ret) {
                        // console.log(data);
                        if (ret.msg == 'ok') {
                            // console.log("11111");
                            $("#c-dh").val(data[0].dh);

                            return false;
                        } else {
                            // console.log("2222");
                            return false;
                        }
                        // return false;
                    }, function (ret) { //error
                        // console.log(ret);
                        // console.log("333");
                        return false;
                    });
                });


                Form.api.bindevent($("form[role=form]"));





            },
            queryString: function () {
                return location.search.replace("dialog=1", "").split('&').filter(function (item) {
                    return !!item;
                }).join("&");
            }
        }
    };
    Controller._queryString = Controller.api.queryString();
    return Controller;
});